package com.dongway.netcondition.socketToBox;

import java.net.Socket;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongway.core.utils.ApplicationContextUtils;
import com.dongway.netcondition.model.DevInfo;
import com.dongway.netcondition.model.DevNetState;
import com.dongway.netcondition.service.DevNetStateService;

public class StartThread {

    private static final Logger logger = LoggerFactory.getLogger(StartThread.class);

    private DevNetStateService devNetStateService = ApplicationContextUtils
        .getBean(DevNetStateService.class);


    public void serverListener() {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Server.startServer();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }


    public void getIpPhone(final int dev_id) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        Socket socket = SocketContent.getInstance().getSocket(dev_id);
                        if (socket != null)
                            GetIpPhone.getIpPhone(dev_id);
                        else
                            System.out.println("设备没在线，请检测设备是否连接！");
                    }

                } catch (Exception e) {

                }
            }
        }).start();
    }


    public static void getNetWork(final int dev_id) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(100);
                        Socket socket = SocketContent.getInstance().getSocket(dev_id);
                        if (socket != null)
                            GetNetWork.getInfor(dev_id);
                        else
                            System.out.println("设备没在线，请检测设备是否连接！");
                    }

                } catch (Exception e) {

                }
            }
        }).start();
    }


    public static void getDevIsListeren(final int dev_id) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        Socket socket = SocketContent.getInstance().getSocket(dev_id);
                        if (socket != null)
                            GetDevIsListeren.getDevIsListeren(dev_id);
                        else
                            System.out.println("设备没在线，请检测设备是否连接！");
                    }

                } catch (Exception e) {

                }
            }
        }).start();
    }


    public static void setIpPhone(final DevInfo devInfo) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    Socket socket = SocketContent.getInstance().getSocket(devInfo.getId());
                    if (socket != null)
                        SetIpPhone.setIpPhone(devInfo);
                    else
                        System.out.println("设备没在线，请检测设备是否连接！");

                } catch (Exception e) {

                }
            }
        }).start();
    }


    public static void setNetWork(final DevInfo devInfo) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    Socket socket = SocketContent.getInstance().getSocket(devInfo.getId());
                    if (socket != null)
                        SetNetwork.setNetwork(devInfo);
                    else
                        System.out.println("设备没在线，请检测设备是否连接！");
                } catch (Exception e) {

                }
            }
        }).start();
    }


    public static void setDevIsListeren(final DevInfo devInfo) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    Thread.sleep(5000);
                    Socket socket = SocketContent.getInstance().getSocket(devInfo.getId());
                    if (socket != null)
                        SetDevIsListeren.setDevIsListeren(devInfo);
                    else
                        System.out.println("设备没在线，请检测设备是否连接！");

                } catch (Exception e) {

                }
            }
        }).start();
    }


    public static void reStart(final int dev_id) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    Thread.sleep(5000);
                    Socket socket = SocketContent.getInstance().getSocket(dev_id);
                    if (null != socket)
                        ReStart.reStart(dev_id);
                    else
                        System.out.println("设备没在线，请检测设备是否连接！");

                } catch (Exception e) {

                }
            }
        }).start();
    }


    public void startListener() {

        new Thread(new Runnable() {

            @Override
            public void run() {

                while (true) {

                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    List<Integer> ids = SocketContent.getInstance().onlines();
                    int number = 0;
                    for (Integer id : ids) {
                        DevNetState devNetState = SocketContent.getInstance().isOnline(id);
                        if (0 == devNetState.getState()) {
                            number++;
                        } else {
                            number--;
                        }
                        devNetStateService.update(devNetState);
                    } // for
                    logger.info("设备在线数量{}个", number);
                } // while
            } // run
        }).start();
    }

}
